﻿/*    file: main.cpp
 *    desc:
 * 
 * created: 2016-04-06
 *  author: chuanjiang.zh@qq.com
 * company: 
 */

#include <stdio.h>
#include <iostream>

#include "BasicType.h"

#include "TFileUtil.h"
#include <assert.h>

#include "TStringUtil.h"
#include "Path.h"
#include "TSingleton.h"
#include "DateTime.h"

#include "CLog.h"

#include "TFileWriter.h"
#include "FfMediaInfo.h"

#include "OptionParser.h"
#include "json/json.h"



int main(int argc, char** argv)
{
	OptionParser parser("ffinfo file [options]", "1.0.0");
	parser.enableHelp();
	parser.addOption('j', "json", "json format");

	if (argc <= 1)
	{
		parser.usage();
		return EINVAL;
	}

	if (!parser.parse(argc, argv))
	{
		parser.usage();
		return EINVAL;
	}

	if (parser.hasHelpOption())
	{
		parser.usage();
		return ENODEV;
	}

	bool isJson = parser.hasOption("json");

	std::string filepath = argv[1];

	FfMediaFormat	format = FfMediaFormat();
	int frameCount = 0;

	int ret = ff_getMediaInfo(filepath.c_str(), &format, &frameCount);
	if (ret == 0)
	{
		if (isJson)
		{
			Json::Value json;

			json["duration"] = format.duration / 1000;

			json["codec"] = format.codec;
			json["width"] = format.width;
			json["height"] = format.height;
			json["framerate"] = format.framerate;
			json["bitrate"] = format.bitrate;
			json["profile"] = format.profile;
			json["level"] = format.level;
			json["bframe"] = format.bframe;

			json["audioCodec"] = format.audioCodec;
			json["channels"] = format.channels;
			json["samplerate"] = format.samplerate;
			json["audioProfile"] = format.audioProfile;
			json["audioBitrate"] = format.audioBitrate;

			json["frameCount"] = frameCount;
			json["videoDuration"] = format.videoDuration / 1000;


			std::string str = json.toStyledString();
			std::cout << str << std::endl;
		}
		else
		{
			printf("duration: %d\n", format.duration / 1000);

			printf("codec: %d\n", format.codec);
			printf("width: %d\n", format.width);
			printf("height: %d\n", format.height);
			printf("framerate: %f\n", format.framerate);
			printf("bitrate: %d\n", format.bitrate);
			printf("profile: %d\n", format.profile);
			printf("level: %d\n", format.level);
			printf("bframe: %d\n", format.bframe);

			printf("audioCodec: %d\n", format.audioCodec);
			printf("channels: %d\n", format.channels);
			printf("samplerate: %d\n", format.samplerate);
			printf("audioProfile: %d\n", format.audioProfile);
			printf("audioBitrate: %d\n", format.audioBitrate);

			printf("frameCount: %d\n", frameCount);
			printf("videoDuration: %d\n", format.videoDuration / 1000);
		}
	}
	return ret;
}



